1. /* sxfcvbin.cpp by K.Tsuru */
  2. // function ID = 502 BRADIX
  3. /********************************************
  4. SDecimal class
  5. Provides radix conversion DRADIX --> BRADIX.
  6. ********************************************/
  7. #ifndef SN_H
  8. #include "sn.h"
  9. #endif
  10. SDecimal SDecimal::ConvToBin(const SDouble& m){
  11. SDecimal result; // result = 0;
  12. if(m.Type() == m.BIN_DEC){ //already converted
  13. result = m;
  14. return result;
  15. }
  16. if(m.Sign(502) == 0) return result;
  17. int e = m.NetRdxExp(); // m = 0.M*DRADIX^e
  18. // 1 1-e
  19. // m = 0.aaaa|bbbb|...*DRADIX^e = 0.0000|...|aaaa|bbbb|....
  20. // out of effective figures ?
  21. if( ( e < 0 ) && ( uint(1-e) > m.MaxSize() +1u) ) return result;
  22. //Check the integral part.
  23. double ipD;
  24. fType ip = 0;
  25. SDouble x(Dabs(m)); //It makes a copy.
  26. if(e >= 1){
  27. SDouble ipSD; //integral part
  28. x = Modf(x, ipSD); //It divides x into integer and decimal.
  29. ipD = doubleD(ipSD, 0); //convert into double
  30. if(ipD >= BRADIX) m.SetError(m.OVERFLOW_ERR,"SX ConvertToBin", 502);
  31. ip = (fType)ipD; // ip < BRADIX
  32. }
  33. //Here x has the decimal part only.
  34. if(x.Sign(502) == 0){ //integlal part only
  35. result.figure[0] = ip;
  36. result.SetSign((int)ip*m.Sign());
  37. return result;
  38. }
  39. //Conversion of decimal part
  40. int f = (int)x.First();
  41. const fType* xv = x.ReadFigures();
  42. fType* rv = result.figure.Elements();
  43. register int i = (int)x.Last();
  44. //value of exponent
  45. e = x.NetRdxExp(); // e is not positive.
  46. #ifndef NDEBUG
  47. assert(e <= 0);
  48. #endif
  49. result.SetSign(1);
  50. rv[0] = xv[i--];
  51. while(i >= f){
  52. //The function XsDiv() does not change the address "rv".
  53. XsDiv(result, DRADIX, result);
  54. if(xv[i]){
  55. result.aTail = 0;
  56. rv[0] += xv[i]; //rv[0] < BRADIX, xv[i]<DRADIX
  57. }
  58. i--;
  59. }
  60. /*
  61. The contribution from exponent.
  62. It divides by DRADIX (1-e) times for two reasons
  63. 1.converting finite decimal such as 2^(-10) exactly.
  64. 2.avoiding the multiplication by an infinite decimal 1/DRADIX in binary
  65. which costs time.
  66. */
  67. for(i = 0; i < 1-e; i++) XsDiv(result, DRADIX, result);
  68. int sgn=result.Sign(502); //maybe result == 0 by above division
  69. //It adds the integral part.
  70. if(ip){
  71. result.aTail = 0;
  72. result.figure[0] += ip;
  73. sgn=1;
  74. }
  75. if(sgn) result.SetSign( m.Sign() );
  76. return result;
  77. }

sxfcvbin.cpp : last modifiled at 2017/03/13 14:32:02(2,316 bytes)
created at 2015/12/22 16:09:56
The creation time of this html file is 2017/10/27 15:45:59 (Fri Oct 27 15:45:59 2017).